<template>
  <div class="container">
    <div class="app-container">
      <!-- el-button 按钮外面添加一个div，方便给外边距 -->
      <div class="role-operate">
        <el-button type="primary" size="mini" @click="showDialog = true; isAdd =true">添加角色</el-button>
      </div>

      <!-- 表格的动态data属性,表格的列下面可以使用data属性里的数据 -->
      <el-table :data="list">
        <!-- prop属性根据data里的数据渲染生成相对应的表列 label是首列显示的数据 -->
        <el-table-column prop="name" align="center" width="200px" label="角色" />
        <el-table-column prop="state" align="center" width="200px" label="启用">
          <!-- el-tabel-column提供的作用域插槽  -->
          <template v-slot="{row}">
            <span> {{ row.state === 1 ? '已启用' : (row.state === 0 ? '未启用' : '未定义') }} </span>
          </template>
        </el-table-column>
        <el-table-column prop="description" align="center" label="描述" />
        <el-table-column align="center" label="操作">
          <el-button type="text">分配权限</el-button>
          <template v-slot="{row}">
            <!-- 获取相对应的id -->
            <el-button type="text" @click="btnEdit(row.id)">编辑</el-button>
          </template>
          <el-button type="text">删除</el-button>
        </el-table-column>
      </el-table>

      <el-row type="flex" style="height:60px" align="middle" justify="end">
        <!-- 放置分页组件 分页组件中的page-size属性只用来计算页码数  不能控制实际展现条数  -->
        <el-pagination
          layout="prev,pager,next"
          :page-size="pageParmas.pagesize"
          :current-page="pageParmas.page"
          :total="total"
          @current-change="changePage"
        />
      </el-row>

      <el-dialog width="500px" :title="isAdd ? '新增角色' : '修改角色' " :visible.sync="showDialog" @close="btnCancel">
        <el-form ref="roleForm" label-width="120px" :model="roleForm" :rules="roleRules">
          <el-form-item size="mini" label="角色名称" prop="name">
            <el-input v-model="roleForm.name" />
          </el-form-item>
          <el-form-item size="mini" label="启用" prop="state">
            <el-switch v-model="roleForm.state" :active-value="1" :inactive-value="0" />
          </el-form-item>
          <el-form-item size="mini" label="角色描述" prop="description">
            <el-input v-model="roleForm.description" type="textarea" rows="3" />
          </el-form-item>
          <el-form-item>
            <el-row type="flex" justify="center">
              <el-col :span="14">
                <el-button size="small" type="primary" @click="btnOk">确认</el-button>
                <el-button size="small" @click="btnCancel">取消</el-button>
              </el-col>
            </el-row>
          </el-form-item>
        </el-form>
      </el-dialog>
    </div>
  </div>
</template>
<script>
import { getRoleList, addRole, updateRole, getRole } from '@/api/role'
export default {
  name: 'Role',
  data() {
    return {
      list: [],
      pageParmas: {
        pagesize: 5,
        page: 1
      },
      total: 0,
      showDialog: false,
      roleForm: {
        name: '',
        state: 0,
        description: ''
      },
      roleRules: {
        name: [{ required: true, message: '请输入名字', trigger: 'blur' }],
        description: [{ required: true, message: '请输入名字', trigger: 'blur' }]
      },
      isAdd: true

    }
  },
  created() {
    this.getRoleList()
  },
  methods: {
    async getRoleList() {
      const { rows, total } = await getRoleList(this.pageParmas)
      // const data = await getRoleList()
      // console.log(data)
      this.list = rows
      this.total = total
    },
    changePage(newpage) {
      this.pageParmas.page = newpage
      this.getRoleList()
    },
    btnOk() {
      this.$refs.roleForm.validate(async isOk => {
        if (isOk) {
          if (this.isAdd) {
            await addRole(this.roleForm)
            this.$message.success('添加角色成功')
          } else {
            console.log(this.roleForm, '请求修改数据')
            await updateRole(this.roleForm.id, this.roleForm)
            this.$message.success('修改角色成功')
          }

          this.getRoleList()
          this.btnCancel()
        }
      })
    },
    btnCancel() {
      this.roleForm = this.$options.data().roleForm // 解决编辑无法重置表单问题
      console.log('点击了取消按钮')
      this.$refs.roleForm.resetFields()
      this.showDialog = false
    },
    async btnEdit(id) {
      this.isAdd = false
      this.roleForm = await getRole(id)
      this.showDialog = true
    }
  }
}

/*  备份 晚自习完成
<!-- prop属性根据data里的数据渲染生成相对应的表列 label是首列显示的数据 -->
         <el-table-column prop="name" align="center" width="200px" label="角色">
          <template v-slot="{ row }">
            <el-input v-if="row.isEdit" />
            <span v-else> {{ row.name }} </span>
          </template>
        </el-table-column>

        <el-table-column prop="state" align="center" width="200px" label="启用">
          <!-- el-tabel-column提供的作用域插槽  -->
          <template v-slot="{row}">
            <el-switch v-if="row.isEdit" />
            <span v-else> {{ row.state === 1 ? '已启用' : (row.state === 0 ? '未启用' : '未定义') }} </span>
          </template>
        </el-table-column>

        <el-table-column prop="description" align="center" label="描述">
          <template v-slot="{ row }">
            <el-input v-if="row.isEdit" />
            <span v-else> {{ row.description }} </span>
          </template>
        </el-table-column>

        <el-table-column align="center" label="操作">
          <template v-slot="{row}">
            <template v-if="row.isEdit">
              <el-button type="primary">确认</el-button>
              <el-button>取消</el-button>
            </template>
            <template v-else>
              <el-button type="text">分配权限</el-button>
              <el-button type="text" @click="btnEditRow(row)">编辑</el-button>
              <el-button type="text">删除</el-button>
            </template>

          </template>
        </el-table-column>
*/
</script>

<style scoped>
.role-operate{
  padding: 10px;
}
</style>

